package com.test.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/7/28 13:42
 */
public class ChannelCopy {
    public static void main(String[] args) throws IOException {
        // source 端也可以介入 文件流
        ReadableByteChannel source = Channels.newChannel(System.in);
        WritableByteChannel dest = Channels.newChannel(System.out);

        copyChannel(source,dest);
    }

    private static void copyChannel(ReadableByteChannel source, WritableByteChannel dest) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//
//        非阻塞模式的通道永远不会
//        让调用的线程休眠。请求的操作要么立即完成，要么返回一个结果表明未进行任何操作。只
        source.read(byteBuffer);

        dest.write(byteBuffer);
        byteBuffer.compact();
    }
}
